/*
 * ExcludeTables.java
 *
 * Created on September 9, 2007, 2:46 AM
 * 16, October, 2009: Class normalized along with the classes ExcludeColumns, ExcludeTables, InputAttribs, TimeLine and StockExperiments in order to allow the creation of an
 * abstract class AttributeFabric, in order to be conformant with design pattern method.
 * 06, April, 2011: Base class no longer inherits from class Table, rather table is now a composition object.
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 *
 * 10, Feb. 2014 Trivial changes in order to make the code compliant with Java 1.7
 */

package portfolio.dbHost.project.attributes;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import portfolio.dbHost.Host;
import portfolio.dbHost.dataBases.DBase;
import portfolio.dbHost.dataBases.dbTable.MysqlTable;
import portfolio.dbHost.dataBases.dbTable.OdbcTable;
import portfolio.dbHost.dataBases.dbTable.Table;

/**
 *
 * @author ACA
 */
public class ExcludeTables /*extends Table*/ implements AbstractAttribute, MouseListener
{
    private Table               table           = null;

    static final public  String TABLE_NAME      = "tablesexclude";
    static final public  int    TABLE_COL_LEN   = 2; //this is not the real size
    static final private String COLUMN_NAMETBL  = "nametbl";
    static final private String TYPE_NAMETBL    = ExcludeTables.COLUMN_NAMETBL + " CHAR(20)";
    
    private Vector vExcludeTables = null;

    /**
     * @Overrides parent's method Table.mousePressed 
     */
    int mouseReleased = 1;    
    public void mouseClicked(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": ExcludeTables:      ========================= Mouse Clicked =========================");
//        throw new UnsupportedOperationException("Not supported yet.");
    }
    public void mousePressed(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": ExcludeTables:      ========================= Mouse Pressed =========================");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": ExcludeTables:      ========================= Mouse Released ========================");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": ExcludeTables:      ========================= Mouse Entered =========================");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": ExcludeTables:      ========================= Mouse Exited  =========================");
//        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    /** Creates a new instance of ExcludeTables */
//    public ExcludeTables(DBase projectParam) throws Exception
//    {
//        super (projectParam, TABLE_NAME);
////        super.setName(TABLE_NAME);
////        super.buildColumns();
//        this.validate ();
//    }
    /**
     * 
     * @param projectParam
     * @throws java.lang.Exception
     */
    public ExcludeTables(DBase projectParam) throws Exception
    {
//        super (projectParam);
//        super.setName(TABLE_NAME);
        int iDBType = projectParam.getDBHost().getDBType();
        if (iDBType == Host.DB_TYPE_MYSQL) {
            table = new MysqlTable (projectParam);
        } else if (iDBType == Host.DB_TYPE_ODBC) {
            table = new OdbcTable (projectParam);
        } else {
            throw new Exception ("Unknown Data Base.");
        }
        table.setName(ExcludeColumns.TABLE_NAME);

        table.getDBase().setTablesVector();
        Vector vTables = table.getDBase().showTables();
        if (vTables == null || !vTables.contains(TABLE_NAME)) {
            this.create ();
        }
    }
    /*
     *
     *
     */
    public void initComponents (String [] sExcludeTables) throws Exception
    {
        
        Vector vExcludeTablesLocal = new Vector ();
        int iExcludesLen = sExcludeTables.length;
        for (int iIdx = 0; iIdx < iExcludesLen; iIdx++) {
            vExcludeTablesLocal.add(sExcludeTables [iIdx]);
        }
        this.insert     (vExcludeTablesLocal);
        
        this.validate ();
    }
    /**
     *
     * @return 
     */
    public DefaultMutableTreeNode processHierarchy ( )
    {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode (TABLE_NAME);
        int iSize = vExcludeTables.size();
                
        for (int iIdx = 0; iIdx < iSize; iIdx++) {
            String sTable = vExcludeTables.get(iIdx).toString();
            DefaultMutableTreeNode nExcludeTbl = new DefaultMutableTreeNode (sTable);
            node.add(nExcludeTbl);
        }
        
        return node;
    }
    /**
     *
     * @param dbTreeParam 
     * @param iXPos 
     * @param iYPos 
     */
    public void getPopupOptions (JTree dbTreeParam, int iXPos, int iYPos)
    {
        JTree dbaseTree = dbTreeParam;       

        JPopupMenu treeOptionsMenu = new JPopupMenu ("Options");
        dbaseTree.add(treeOptionsMenu);
        
        dbaseTree.addMouseListener(this);

        treeOptionsMenu.setLocation (iXPos, iYPos);
        treeOptionsMenu.show (treeOptionsMenu, iXPos, iYPos);
    } /* end method getPopupOptions */      
    /**
     *
     */
    private void validate () throws Exception
    {
        Vector [] vTemp = table.selectAllfromTable();
        vExcludeTables = new Vector ();
   
        int iExcludeTablesCols = vTemp.length;
        for (int iCols = 0; iCols < iExcludeTablesCols; iCols++) {
            int iExcludeTablesRow = vTemp [iCols].size();
            for (int iRows = 0; iRows < iExcludeTablesRow; iRows++) {
                String sTable = vTemp[iCols].get(iRows).toString();
                vExcludeTables.add(sTable);
//                System.out.print (" " + sTable);
            }
//            System.out.println();
        }
    }
    /**
     * Returns ExcludeTables list, in a String array of n rows, distributed as follows:
     * N rows and one column coutaining a list of TableNames.
     * @return 
     */
    public String [] get ()
    {
        String [] sExcludeTables = (String [])vExcludeTables.toArray(new String [0]);
        
        return sExcludeTables;
    }
    /*
     * 
     */
    public Object getTreeObject (Object [] oPathParam)
    {
        if (oPathParam == null) {
            return null;
        }
        if (oPathParam.length < ExcludeTables.TABLE_COL_LEN) {
            return null;
        }
        if (!oPathParam [1].toString().equals(ExcludeTables.TABLE_NAME)) {
           return null;  //error, no database found
        }
        
        return this;
    } /* end method getTreeObject */
    /**
     * 
     */
    public void create () throws Exception
    {
        table.setName(ExcludeTables.TABLE_NAME);
        table.create(ExcludeTables.TYPE_NAMETBL);
    }
    /**
     * 
     * @param vExcludeTablesParam
     * @throws java.lang.Exception
     */
    public void insert (Vector vExcludeTablesParam) throws Exception
    {
        try {
            if (vExcludeTablesParam == null) {
                throw new Exception ("processExcludes: no items to exclude.");
            }
            Connection connection = table.getDBase().getDBHost().getConnection();
            String sSelProj = table.getDBase().getDBName();

            int iSize = vExcludeTablesParam.size ();
            if (iSize > 0) {
                Vector vColExclude = new Vector ();
                Vector vValExclude = new Vector ();
                
                Vector vValTemp = ExcludeTables.read (connection, sSelProj);
                
                for (int i = 0; i < iSize; i++) {
                    vColExclude.add (ExcludeTables.COLUMN_NAMETBL);
                    String sExclude = (String) vExcludeTablesParam.get (i);
                    vValExclude.add ("'" + sExclude + "'");
                    if (vValTemp != null) {
                        if (!vValTemp.contains(sExclude)) {
//                            Table.insert (connection, sSelProj, this.TABLE_NAME ,vColExclude, vValExclude);
                            table.insert(vColExclude, vValExclude);
                        }
                    } else {
//                        Table.insert (connection, sSelProj, this.TABLE_NAME ,vColExclude, vValExclude);
                        table.insert(vColExclude, vValExclude);
                    } 
                    vValExclude.clear ();
                    vColExclude.clear ();
                }
            }
            
            table.buildColumns();
        } catch (Exception e) {
            throw new Exception (e);
        }
    }
    /**
     *
     * @param connection
     * @param sDBName
     * @return 
     * @throws java.lang.Exception
     */
    static public Vector read (Connection connection, String sDBName)  throws Exception
    {
        try {
            if (connection == null)
                throw new Exception ("null connection.");
            
            ResultSet resultSet = Table.getQuery(connection, sDBName, "*", ExcludeTables.TABLE_NAME);
            if (resultSet == null) {
                return null;
            }
            if (resultSet.last() == false) {
                return null;
            }
            resultSet.first();
            Object [][] OMatrix = Table.transferObjectResultSet2Matrix(resultSet, 0);
            
            int iRowLen = OMatrix.length;
            if (iRowLen < 1) {
                return null;
            }
            int iColLen = OMatrix[0].length;
            if (iColLen < 1) {
                return null;
            }
            Vector  vOut = new Vector (); //new String [iRowLen];
//            System.out.println ("[[[[[[[[[[[[[[[[[[[[[[[[[[ iRowLen = " + iRowLen + "; iColLen = " + iColLen + "]]]]]]]]]]]]]]]]]]]]]]]]]]");
            for (int iIdx = 0; iIdx < iRowLen; iIdx++) {
                vOut.add ((String) OMatrix [iIdx][0]);
//                System.out.println ("{ ExcludeTable [" + iIdx + "]: " + sOut [iIdx] + "}");
            }
            
            return vOut; //sOut;
        } catch (Exception ex) {
            throw new Exception (ex);
        }
    }

    public Table getTable() {
        return table;
    }
}
